package com.zhia.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhia.demo.dao.BrowseHistoryDao;
import com.zhia.demo.entity.BrowseHistoryEntity;
import com.zhia.demo.service.BrowseHistoryService;
import org.springframework.stereotype.Service;

/**
 * @author DONG
 * @create 2023/11/5 15:27
 */
@Service
public class BrowseHistoryServiceImpl extends ServiceImpl<BrowseHistoryDao, BrowseHistoryEntity> implements BrowseHistoryService {

    @Override
    public Integer userBrowseChapter(String bookId, Integer userId) {
        BrowseHistoryEntity one = this.getOne(new QueryWrapper<BrowseHistoryEntity>().eq("book_id", bookId).eq("user_id", userId));
        return one != null ? one.getChapter() : null;
    }

    @Override
    public void saveHistory(String bookId, Integer userId, Integer chapter) {
        BrowseHistoryEntity browseHistoryEntity = this.getOne(new QueryWrapper<BrowseHistoryEntity>().eq("book_id", bookId).eq("user_id", userId));
        if (browseHistoryEntity != null) {
            browseHistoryEntity.setChapter(chapter);
            this.updateById(browseHistoryEntity);
        }else {
            BrowseHistoryEntity b = new BrowseHistoryEntity();
            b.setBookId(bookId);
            b.setUserId(userId);
            b.setChapter(chapter);
            this.save(b);
        }
    }


}
